<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Common;

use Srv\Apps\Apis\Common\DefCtl\DefChannelCtl;
use Srv\Apps\Apis\Common\DefCtl\DefPlatformCtl;
use Srv\Apps\Apis\Common\DefCtl\DefUserRegTypeCtl;
use Srv\Apps\Proto\DTO\Common\CommPlatformListDTO;
use Srv\Apps\Proto\DTO\Common\CommRegTypeListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\DTO\Common\CommChannelListDTO;
use Srv\Apps\Proto\VO\Common\CommChannelListVO;
use Srv\Apps\Proto\VO\Common\CommChannelVO;
use Srv\Apps\Proto\VO\Common\CommPlatformListVO;
use Srv\Apps\Proto\VO\Common\CommPlatformVO;
use Srv\Apps\Proto\VO\Common\CommRegTypeListVO;
use Srv\Apps\Proto\VO\Common\CommRegTypeVO;
use Srv\Libs\Common\CommArray;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class CommonAct extends ActionAbstract
{
    /**
     * @url /comm/channel/list
     * @auth LOGIN
     * @req Srv.Apps.Proto.DTO.Common.CommChannelListDTO
     * @rsp Srv.Apps.Proto.VO.Common.CommChannelListVO
     * 获取渠道列表
     */
    public function getChannelList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof CommChannelListDTO) || !($VOClass instanceof CommChannelListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $dataList               = DefChannelCtl::tag()->getDataList();
        $CommChannelListVO      = new CommChannelListVO();
        if(CommArray::isForeach($dataList)){
            $CommChannelVOList  = [];
            foreach ($dataList as $item){
                if(!isset($item['key']) || !isset($item['name'])) continue;
                if($RouteConfData->isSetParam($DTOClass::_key) && strpos($item['key'], $DTOClass->getKey()) === false) continue;
                if($RouteConfData->isSetParam($DTOClass::_name) && strpos($item['name'], $DTOClass->getName()) === false) continue;
                $CommChannelVO          = new CommChannelVO();
                $CommChannelVO->setKey($item['key']);
                $CommChannelVO->setName($item['name']);
                $CommChannelVOList[]    = $CommChannelVO;
            }
            $CommChannelListVO->setList($CommChannelVOList);
        }
        return ResponseData::rspSuccAuto('', $CommChannelListVO);
    }

    /**
     * @url /comm/platform/list
     * @auth LOGIN
     * @req Srv.Apps.Proto.DTO.Common.CommPlatformListDTO
     * @rsp Srv.Apps.Proto.VO.Common.CommPlatformListVO
     * 获取设备平台列表
     */
    public function getPlatformList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof CommPlatformListDTO) || !($VOClass instanceof CommPlatformListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $dataList               = DefPlatformCtl::tag()->getDataList();
        $CommPlatformListVO     = new CommPlatformListVO();
        if(CommArray::isForeach($dataList)){
            $CommPlatformVOList = [];
            foreach ($dataList as $item){
                if(!isset($item['key']) || !isset($item['name'])) continue;
                if($RouteConfData->isSetParam($DTOClass::_key) && strpos($item['key'], $DTOClass->getKey()) === false) continue;
                if($RouteConfData->isSetParam($DTOClass::_name) && strpos($item['name'], $DTOClass->getName()) === false) continue;
                $CommPlatformVO         = new CommPlatformVO();
                $CommPlatformVO->setKey($item['key']);
                $CommPlatformVO->setName($item['name']);
                $CommPlatformVOList[]   = $CommPlatformVO;
            }
            $CommPlatformListVO->setList($CommPlatformVOList);
        }
        return ResponseData::rspSuccAuto('', $CommPlatformListVO);
    }

    /**
     * @url /comm/regtype/list
     * @auth LOGIN
     * @req Srv.Apps.Proto.DTO.Common.CommRegTypeListDTO
     * @rsp Srv.Apps.Proto.VO.Common.CommRegTypeListVO
     * 获取注册方式列表
     */
    public function getRegTypeList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof CommRegTypeListDTO) || !($VOClass instanceof CommRegTypeListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $dataList               = DefUserRegTypeCtl::tag()->getDataList();
        $CommRegTypeListVO      = new CommRegTypeListVO();
        if(CommArray::isForeach($dataList)){
            $CommRegTypeVOList  = [];
            foreach ($dataList as $item){
                if(!isset($item['key']) || !isset($item['name'])) continue;
                if($RouteConfData->isSetParam($DTOClass::_key) && strpos($item['key'], $DTOClass->getKey()) === false) continue;
                if($RouteConfData->isSetParam($DTOClass::_name) && strpos($item['name'], $DTOClass->getName()) === false) continue;
                $CommRegTypeVO          = new CommRegTypeVO();
                $CommRegTypeVO->setKey($item['key']);
                $CommRegTypeVO->setName($item['name']);
                $CommRegTypeVOList[]    = $CommRegTypeVO;
            }
            $CommRegTypeListVO->setList($CommRegTypeVOList);
        }
        return ResponseData::rspSuccAuto('', $CommRegTypeListVO);
    }
}